package com.ruoyi.project.his.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.his.domain.Doctor;
import com.ruoyi.project.his.domain.DoctorDept;
import com.ruoyi.project.his.mapper.DoctorDeptMapper;
import com.ruoyi.project.his.mapper.DoctorMapper;
import com.ruoyi.project.his.service.IDoctorDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname DdoctorDeptServiceImpl
 * @Description
 * @Date 2022/6/27 16:06
 * @Author Lee
 */
@Service
public class DoctorDeptServiceImpl implements IDoctorDeptService {
    @Autowired
    private DoctorDeptMapper doctorDeptMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Override
    public List<DoctorDept> selectDoctorDeptList(DoctorDept doctorDept) {
        return doctorDeptMapper.selectDoctorDeptList(doctorDept);
    }

    @Override
    public List<DoctorDept> selectDoctorDeptAll() {
        return doctorDeptMapper.selectDoctorDeptAll();
    }

    @Override
    public int insertDoctorDept(DoctorDept doctorDept) {
        return doctorDeptMapper.insertDoctorDept(doctorDept);
    }

    @Override
    public int updateDoctorDept(DoctorDept doctorDept) {
        return doctorDeptMapper.updateDoctorDept(doctorDept);
    }

    @Override
    public DoctorDept getDoctorDeptById(Integer id) {
        return doctorDeptMapper.getDoctorDeptById(id);
    }

    @Override
    public int deleteDoctorDeptByDeptIds(Integer[] ids) {
        for (Integer id : ids) {
            List<Doctor> doctors = doctorMapper.SelectDoctorByDeptId(id);
            if(doctors.size()>0){
                throw new CustomException("科室中存在医生,不能删除");
            }
        }
        return doctorDeptMapper.deleteDoctorDeptByDeptIds(ids);
    }

    @Override
    public String checkDocketDeptNameUnique(DoctorDept doctorDept) {
        Integer deptId = StringUtils.isNull(doctorDept.getDeptId()) ? -1 : doctorDept.getDeptId(); // 获取主键。
        DoctorDept info = doctorDeptMapper.checkDocketDeptNameUnique(doctorDept.getDeptName());
        if(StringUtils.isNotNull(info) && !deptId.equals(info.getDeptId())){ // 判断查询结果非空，且主键不同。
            return UserConstants.NOT_UNIQUE; // 不唯一
        }
        return UserConstants.UNIQUE; // 唯一
    }

    @Override
    public String checkDocketDeptCodeUnique(DoctorDept doctorDept) {
        Integer deptId = StringUtils.isNull(doctorDept.getDeptId()) ? -1 : doctorDept.getDeptId(); // 获取主键。
        DoctorDept info = doctorDeptMapper.checkDocketDeptCodeUnique(doctorDept.getDeptCode());
        if(StringUtils.isNotNull(info) && !deptId.equals(info.getDeptId())){ // 判断查询结果非空，且主键不同。
            return UserConstants.NOT_UNIQUE; // 不唯一
        }
        return UserConstants.UNIQUE; // 唯一
    }
}
